#!/usr/bin/perl5.8.4
use strict;
use Tk;
use Tk::Pane;


my ($titlelinestring,@AoH,@titleline,$temp,$i,$linecount,$no_of_cols,$j);
my ($pane,$ff,$frame,$mw,$entry,$sb,$sbb,$fullframe);

($titlelinestring, @AoH)=readrdb($ARGV[0]);
@titleline=split /\t/, $titlelinestring;
$linecount=@AoH;
$no_of_cols=@titleline;

$mw=MainWindow->new;

my $pane=$mw->Scrolled('Pane',-scrollbars=>'soe');

$pane->pack();

for($i=0;$i<$linecount;$i++)
{
	$frame=$pane->Frame;
	for($j=0;$j<$no_of_cols;$j++)
	{
		$entry=$frame->Entry(-textvariable=>\$AoH[$i]{$titleline[$j]});
		$entry->pack(-side=>"left");
	}
	print "Adding Row $i\n";
	$frame->pack;
}

MainLoop;

#$titleline[$no_of_cols]="IBIS";
#$AoH[0]->{"IBIS"}="IBIS";
#$AoH[1]->{"IBIS"}="S";
#for($i=2;$i<$linecount;$i++)
#{
#	$AoH[$i]->{"IBIS"}="true";
#}

$titlelinestring=join("\t",@titleline);
writerdb($titlelinestring,@AoH);


# Do not touch anything below this line unless you know what you are doing

sub readrdb
{
	my ($filename,$counter,$titleline,@words,$line,@dataline,@datatypeline,$linecount,$myhash,@titleline,$i,@AoH);
	($filename)=@_;
	open(INPUT,"< $filename");

	$linecount=0;
	while(<INPUT>)
	{
		$line=$_;
		@words=split /\t/, $line;
		$myhash={};
		if($words[0]!~m/\#.*/)
		{
			$counter=@words;
			
			if($linecount==0)
			{
				@titleline=@words;
			}
			
			if($linecount==1)
			{
				@datatypeline=@words;
			}
			
			if($linecount>1)
			{
				@dataline=@words;
			}	

			for($i=0;$i<$counter;$i++)
			{
				chomp($words[$i]);
				if($words[$i] eq "")
				{
					chomp($titleline[$i]);
					$myhash->{$titleline[$i]}="_nil_";
				}
				else
				{
					chomp($titleline[$i]);
					$myhash->{$titleline[$i]}=$words[$i];
				}	
			}
			push @AoH,$myhash;
			$linecount++;
		}
	}
	close(INPUT);
	return(join("\t",@titleline),@AoH);
}



sub writerdb
{
	my (@titleline,$counter,$linecount,$a,$i,$titleline,$titlelinestring,@AoH);
	
	($titlelinestring,@AoH)=@_;


	@titleline=split /\t/, $titlelinestring;
	$counter=@titleline;

	$linecount=@AoH;

	foreach($a=0;$a<$linecount;$a++)
	{
		for($i=0;$i<$counter;$i++)
		{
			if($AoH[$a]{$titleline[$i]} eq "_nil_")
			{
				print "";
			}
			else
			{
				chomp($AoH[$a]{$titleline[$i]});
				print $AoH[$a]{$titleline[$i]};
			}	
			if($i<$counter-1)
			{
				print "\t";
			}
			else
			{
				print "\n";
			}	
		}
	}
}
